import java.util.Arrays;

public class CopyArray {
    //方法1：新增一个数组，通过遍历原数组为新数组赋值完成数组拷贝
    public static void main1(String[] args) {
        int[] array1 = {1,2,3,4,5,6};

        int[] array2 = new int[array1.length];

        for (int i = 0; i < array1.length; i++) {
            array2[i] = array1[i];
        }
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));
    }


    //方法2：利用Java编译器的方法Array.copyOf方法完成数组拷贝
    public static void main2(String[] args) {
        int[] array1 = {2,3,4,5,6};
        int[] array2 = Arrays.copyOf(array1,array1.length);
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));
    }


    public static void main(String[] args) {
        int[] array  = {1,2,3,4};
        int[] copy =  new int[array.length];
        System.arraycopy(array,0,copy,0,array.length);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(copy));
    }
}
